package com.harmonk.firstscene.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import androidx.room.Delete
import androidx.room.OnConflictStrategy
import androidx.room.Transaction
import com.harmonk.firstscene.db.entity.EventNote
import com.harmonk.firstscene.db.entity.EventWithAll
import com.harmonk.firstscene.db.entity.Person
import com.harmonk.firstscene.db.entity.TrafficEvent
import com.harmonk.firstscene.db.entity.Vehicle
import kotlinx.coroutines.flow.Flow

@Dao
interface TrafficEventDao {
    @Query("SELECT * FROM traffic_events ORDER BY timestamp DESC")
    fun getAllTrafficEvents(): Flow<List<TrafficEvent>>

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun insertTrafficEvent(trafficEvent: TrafficEvent): Long

    @Update
    suspend fun updateTrafficEvent(trafficEvent: TrafficEvent)

    @Delete
    suspend fun deleteTrafficEvent(trafficEvent: TrafficEvent)

    // 监听主事件变化
    @Query("SELECT * FROM traffic_events WHERE eventId = :eventId")
    fun getTrafficEvent(eventId: Long): Flow<TrafficEvent>

    // 监听车辆变化
    @Query("SELECT * FROM vehicles WHERE eventId = :eventId")
    fun getVehiclesForEvent(eventId: Long): Flow<List<Vehicle>>

    // 监听人员变化
    @Query("SELECT * FROM persons WHERE eventId = :eventId")
    fun getPersonsForEvent(eventId: Long): Flow<List<Person>>

    // 监听事件备注变化
    @Query("SELECT * FROM event_notes WHERE eventId = :eventId")
    fun getNotesForEvent(eventId: Long): Flow<List<EventNote>>
}